/**
 * Created by AMD on 31.10.14.
 */
package layers {
import ShopObjects.AbstractGameObjectView;
import ShopObjects.DoorView;


import flash.geom.Point;


import starling.display.Sprite;
import starling.filters.ColorMatrixFilter;

public class GameLayer extends Sprite{
    private var _shopView:ShopView = null;

    private var _sortableObjects:Vector.<AbstractGameObjectView> = new Vector.<AbstractGameObjectView>();
    private var _doors:Vector.<DoorView> = new <DoorView>[];

    //private static var _doorhighlightFilter:ColorMatrixFilter = null
    public function GameLayer() {
        this._shopView = new ShopView();
        this.addChild(this._shopView);

    }


    private var _shopViewIndex:int
    public function addDoor(door:DoorView):Boolean{
        var i:int = 0;
        for(i = 0;i <this._doors.length; i ++){
            if(this._doors[i] == door){
                return false;
            }
        }
        this._shopViewIndex = this.getChildIndex(this._shopView);
        this._doors.push(door);
        this.addChildAt(door, this._shopViewIndex + 1);
        return true;
    }
    public function removeAllDoors():void{
        trace("Deleting All Doors");
        while(_doors.length > 0){
            this.removeDoor(_doors[0]);
        }
    }

    private var _bufDoorView:DoorView;
    public function setDoorState(doorCoords:Point, opened:Boolean):Boolean {
        this._bufDoorView = getDoorOnQuadrant(doorCoords);
        if(this._bufDoorView){
            if(this._bufDoorView.opened != opened){
                this._bufDoorView.opened = opened;
                return true;//state changed
            }
        }
        return false;// state not changed (or door not exist)
    }

    public function removeDoor(door:DoorView):void {
        var i:int = 0;
        for(i = 0;i <this._doors.length; i ++){
            if(this._doors[i] == door){
                this._doors.splice(i,1);
                break;
            }
        }
        door.parent.removeChild(door);
        door.dispose();
    }
    public function getDoorOnQuadrant(quadrantCoords:Point):DoorView{
        var i:int = 0;
        var  _bufPoint:Point = new Point();
        var _bufDoor:DoorView;
        var _bufQuadCoords:Point = quadrantCoords.clone();

        if(_bufQuadCoords.x == -1) _bufQuadCoords.x = 0;//new coords type
        if(_bufQuadCoords.y == -1) _bufQuadCoords.y = 0;

        for(i = 0;i <this._doors.length; i ++){
            _bufDoor = this._doors[i];
            _bufPoint.x = _bufDoor.x;
            _bufPoint.y = _bufDoor.y;
            _bufPoint = this._shopView.getQuadrant(_bufDoor.parent.localToGlobal(_bufPoint));
            if(_bufPoint.equals(_bufQuadCoords)){
                return _bufDoor;
                break;
            }
        }
        return null;
    }

    public function removeDoorsHighlighting():void {
        for(var i:int = 0;i <this._doors.length; i ++){
            this._doors[i].doorImage.filter = null;
        }
    }
    public function highlightDoorOnQuad(quad:Point):void {
        this._bufDoorView = getDoorOnQuadrant(quad);
        if(_bufDoorView){
            //if(_doorhighlightFilter == null){
                var _doorhighlightFilter:ColorMatrixFilter = new ColorMatrixFilter();
                _doorhighlightFilter.adjustBrightness(0.2);
            //}
            this._bufDoorView.doorImage.filter = _doorhighlightFilter;
        }
    }


    public function addSortableObject(gameObject:AbstractGameObjectView):Boolean{
        var i:int = 0;
        for(i = 0;i <this._sortableObjects.length; i ++){
            if(this._sortableObjects[i] == gameObject){
                return false;
            }
        }
        this._sortableObjects.push(gameObject);
        this.addChild(gameObject);
        return true;
    }
    public function removeAllSortableObjects():void{
        trace("Deleting All Static Objects");
        while(_sortableObjects.length > 0){
            this.removeSortableObject(_sortableObjects[0]);
        }
    }
    public function removeSortableObject(gameObject:AbstractGameObjectView):void{
        var i:int = 0;
        for(i = 0;i <this._sortableObjects.length; i ++){
            if(this._sortableObjects[i] == gameObject){
                this._sortableObjects.splice(i,1);
                break;
            }
        }
        gameObject.parent.removeChild(gameObject);
        gameObject.dispose();
    }

    public function getStaticObjectOnQuadrant(quadrantCoords:Point):AbstractGameObjectView{
        var i:int = 0;
        var  _bufPoint:Point = new Point();
        var _bufObject:AbstractGameObjectView;
        for(i = 0;i <this._sortableObjects.length; i ++){
            _bufObject = this._sortableObjects[i];
            _bufPoint.x = _bufObject.x;
            _bufPoint.y = _bufObject.y;
            _bufPoint = this._shopView.getQuadrant(_bufObject.parent.localToGlobal(_bufPoint));
            if(_bufPoint.equals(quadrantCoords)){
                return _bufObject;
                break;
            }
        }
        return null;
    }

    public function sortObjectsZOrder2():void{// native sort method
        this.sortChildren(_zOrderSortFunction); //Uses a lot of Draw Calls!!!
    }
    public function sortObjectsZOrder():void{ // faster than native sort method!
        this._sortableObjects = this._sortableObjects.sort(this._zOrderSortFunction)
        for(var i:int = 0; i<this._sortableObjects.length; i++){
            this._sortableObjects[i].parent.addChild(this._sortableObjects[i]);
        }
    }
    private var _bufPointA:Point = new Point();
    private var _bufPointB:Point = new Point();
    private function _zOrderSortFunction(a:AbstractGameObjectView, b:AbstractGameObjectView):int{
        shopView.getQuadrantToPoint((a.position),_bufPointA);
        shopView.getQuadrantToPoint((b.position),_bufPointB);
        if((_bufPointA.x + _bufPointA.y) > (_bufPointB.x + _bufPointB.y)){
            return 1;
        }
        if((_bufPointA.x + _bufPointA.y) < (_bufPointB.x + _bufPointB.y)){
            return -1;
        }
        return 0;
    }

    public function getLocalCoordsByQuadrant(quadrantCoords:Point):Point{
        return globalToLocal(this._shopView.isometricToScreen(this._shopView.quadrantToIsometric(quadrantCoords)));
    }

    public function get shopView():ShopView {
        return _shopView;
    }

    public function get sortableObjects():Vector.<AbstractGameObjectView> {
        return _sortableObjects;
    }


    public function get shopViewTopIndex():int {
        this._shopViewIndex = this.getChildIndex(this._shopView);

        return _shopViewIndex + this._doors.length;
    }
}
}
